using System;
using EPiServer.Common;

namespace EPiAbstractions.Common
{
    public class SiteHandlerFacade : ISiteHandlerFacade
    {
        private static SiteHandlerFacade _instance;

        public SiteHandlerFacade()
        {
            SiteHandler.SiteAdded += OnSiteAdded;
            SiteHandler.SiteRemoved += OnSiteRemoved;
            SiteHandler.SiteUpdated += OnSiteUpdated;
        }

        public static SiteHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new SiteHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region ISiteHandlerFacade Members

        public event SiteEventHandler SiteAdded;
        public event SiteEventHandler SiteRemoved;
        public event SiteEventHandler SiteUpdated;

        public virtual ISite GetSite(Int32 siteId)
        {
            return SiteHandler.GetSite(siteId);
        }

        public virtual ISite ResolveSite(String alias)
        {
            return SiteHandler.ResolveSite(alias);
        }

        public virtual SiteCollection GetSiteList()
        {
            return SiteHandler.GetSiteList();
        }

        public virtual ISite AddSite(ISite site)
        {
            return SiteHandler.AddSite(site);
        }

        public virtual void UpdateSite(ISite site)
        {
            SiteHandler.UpdateSite(site);
        }

        public virtual void RemoveSite(ISite site)
        {
            SiteHandler.RemoveSite(site);
        }

        public virtual ISiteAlias GetSiteAlias(Int32 id)
        {
            return SiteHandler.GetSiteAlias(id);
        }

        #endregion

        public virtual void OnSiteAdded(SiteEventArgs args)
        {
            if (SiteAdded != null)
                SiteAdded(args);
        }

        public virtual void OnSiteRemoved(SiteEventArgs args)
        {
            if (SiteRemoved != null)
                SiteRemoved(args);
        }

        public virtual void OnSiteUpdated(SiteEventArgs args)
        {
            if (SiteUpdated != null)
                SiteUpdated(args);
        }
    }
}